/*
   Copyright (C) 2007 Yen-Ju Chen <yjchenx gmail>

   This application is free software; you can redistribute it and/or 
   modify it under the terms of the MIT license. See COPYING.

*/

#import "AppController.h"

@implementation AppController
- (IBAction) showNeighbors: (id) sender
{
	NSLog(@"%@", NSStringFromSelector(_cmd));
}

- (IBAction) showMainWindow: (id) sender
{
	[mainController showWindow: sender];
}

- (void) awakeFromNib
{
}

/* NSApplication */
- (void) applicationWillFinishLaunching: (NSNotification *) not
{
	NSArray *array  = [[BKPluginRegistry sharedInstance] loadedValidOrderedExtensionsFor: @"org.etoile-project.organizekit.blocks" protocol: @protocol(NSObject)];
	int i, count = [array count];
	BKExtension *extension;

	/* Get data source */
	for (i = 0; i < count; i++)
	{
		extension = [array objectAtIndex: i];
		if ([[extension extensionClass] conformsToProtocol: @protocol(KRDataSourceProtocol)])
		{
			dataSource = [extension extensionInstance];
			break;
		}
	}

	/* Get main window */
	for (i = 0; i < count; i++)
	{
		extension = [array objectAtIndex: i];
		if ([[extension extensionClass] conformsToProtocol: @protocol(KRWindowControllerProtocol)])
		{
			mainController = [extension extensionInstance];
			break;
		}
	}
	if ((dataSource == nil) || (mainController == nil))
	{
		NSLog(@"Internal Error: Cannot get data source or main window controller");
		[NSApp terminate: self];
	}
	[mainController setDataSource: dataSource];
	[mainController setRootGroup: nil];
}

- (void) applicationDidFinishLaunching: (NSNotification *) not
{
	[self showMainWindow: self];
}

- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) app
{
	return NSTerminateNow;
}

- (id) init
{
	self = [super init];
	return self;
}

- (void) dealloc
{
	[super dealloc];
}

@end

